package cn.ppwx.crm.modules.sys.service;

import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.ppwx.crm.common.log.CrmLogger;


@Service
public class WorkFlowService {
	
	
	@Autowired
	private RepositoryService  repositoryService;
	
	@Autowired
	private TaskService taskService;
	
	
	/**部署流程定义*/
	public void deploymentProcessDefinition(){
		Deployment deployment = repositoryService.createDeployment()
						.name("leave")
						.addClasspathResource("diagrams/leave.bpmn")
						.addClasspathResource("diagrams/leave.png")
						.deploy();//完成部署
		CrmLogger.log.info("部署ID："+deployment.getId());
		CrmLogger.log.info("部署名称："+deployment.getName());
	}
	
	
	
	/**部署流程定义*/
	public void deploymentPurchaseProcessDefinition(){
		Deployment deployment = repositoryService.createDeployment()
						.name("purchase")
						.addClasspathResource("diagrams/purchase.bpmn")
						.addClasspathResource("diagrams/purchase.png")
						.deploy();//完成部署
		CrmLogger.log.info("部署ID："+deployment.getId());
		CrmLogger.log.info("部署名称："+deployment.getName());
	}
	
	
	//完成审批流程
	public void completeTask(String taskId,String message){
		
		String processInstanceId = taskService.createTaskQuery().taskId(taskId).singleResult().getProcessInstanceId();
		taskService.addComment(taskId, processInstanceId, message);
		taskService.complete(taskId);
	}
	
	
	//完成审批流程
	public void completeTask(String taskId,Map<String, Object> variables,String message){
		
		String processInstanceId = taskService.createTaskQuery().taskId(taskId).singleResult().getProcessInstanceId();
		taskService.addComment(taskId, processInstanceId, message);
		taskService.complete(taskId,variables);
	}
	

}
